//
//  ProvisionModule.m
//  AwesomeProject
//
//  Created by aiquantong on 5/12/16.
//  Copyright © 2016 Facebook. All rights reserved.
//

#import "ProvisionManager.h"
#import "KandyAdpter.h"
#import "AccessManager.h"
#import "SessionMangementManager.h"

#import "AppDelegate+Push.h"

@interface ProvisionManager()

@end

@implementation ProvisionManager


static ProvisionManager *shareInstance = nil;

+(ProvisionManager *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (shareInstance == nil) {
            shareInstance = [[ProvisionManager alloc] init];
        }
    });
    return shareInstance;
}


- (instancetype)init
{
  self = [super init];
  if (self) {
    
  }
  return self;
}


-(void)requestCodeBySMSCN:(NSString *)phoneNumber callback:(KandyCallback)callback
{
    KandyAreaCode *kandyAreaCode = [[KandyAreaCode alloc] initWithISOCode:@"cn" andCountryName:@"" andPhonePrefix:@""];
    if (![phoneNumber hasPrefix:@"86"]) {
        phoneNumber = [NSString stringWithFormat:@"86%@",phoneNumber];
    }
    [[Kandy sharedInstance].provisioning
     requestCode:kandyAreaCode
     phoneNumber:phoneNumber
     codeRetrivalMethod:EKandyValidationMethod_sms
     responseCallback:^(NSError *error, NSString *destinationToValidate) {
         KDALog(@"error === %@ destinationToValidate == %@", [error description], destinationToValidate);
         if(callback) {
             callback(error);
         }
     }];
}


-(void)validate:(NSString *)txtOTP phoneNumber:(NSString *)phoneNumber callback:(KandyUserCallback)callback
{

    KandyAreaCode *kandyAreaCode = [[KandyAreaCode alloc] initWithISOCode:@"cn" andCountryName:@"" andPhonePrefix:@""];
    [[Kandy sharedInstance].provisioning
     validateAndProvision:txtOTP
     destination:phoneNumber
     areaCode:kandyAreaCode
     responseCallback:^(NSError *error, KandyUserInfo *provisionedUserInfo) {
         KDALog(@"error === %@ provisionedUserInfo == %@", [error description], provisionedUserInfo);
         if (callback) {
             callback(error, provisionedUserInfo);
         }
     }];
}


+(void) getUserDetails:(NSString *)userId callback:(KandyCallback)callback
{
  [[Kandy sharedInstance].provisioning
   getUserDetails:userId
   responseCallback:^(NSError *error, KandyUserInfo *userProvisionInfo) {
     KDALog(@"error === %@ destinationToValidate == %@", [error description], [userProvisionInfo description]);
       if(callback) callback(error);
   }];
}


+(void)deactivate:(KandyCallback)callback
{
      [[Kandy sharedInstance].provisioning deactivateWithResponseCallback:^(NSError *error) {
          KDALog(@"error === %@ ", [error description]);
          [SessionMangementManager removeSaveSession];
          [[KandyAdpter shareInstance] reinitKandySDK];
          if (callback) {
              callback(error);
          }
      }];
}

@end


